const { json } = require('express');
const { existsSync } = require('fs');
const { data } = require('jquery');

var ipcM = require('electron').ipcMain;

ipcM.on('connTest', function(event, arg) {
    var static = require('./static')
    var sqlhelper = require('./sqlhepler');
    // console.log(arg);

    var connConfig = {
        host : arg.host,
        port : arg.port,
        user : arg.user,
        password : arg.pwd,
        database : arg.database
    }
    static.connConfig = connConfig;
    sqlhelper.init(
    function(){
        event.returnValue = 'fail';
    },
    function(){
        event.returnValue = 'success';
    });

});

ipcM.on('readExcel', function(event, path) {
    console.log(path);
    var xlsx = require('node-xlsx');//ANSI
    var obj = xlsx.parse(path);

    var sqlhelper = require('./sqlhepler');
    var sheets = sqlhelper.toSqlSheets(obj,'utf8');

    var promises = [];

    for(var i=0;i<sheets.length;i++){
        var body = sqlhelper.createSheet(sheets[i]);
        var obj = sqlhelper.excute(body);
        promises.push(obj);
    }
    Promise.all(promises).then((values)=>{
        console.log('all:');
        console.log(values);
        event.returnValue = values;
    }) 
});
